import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

/// Flutter 87: 初识状态管理 Bloc (二)
/// https://www.jianshu.com/p/b8f3aeda6551

/// https://pub.flutter-io.cn/packages/flutter_bloc/versions/2.1.1/example 官方示例
///
/// A [StatelessWidget] which uses:
//  * [bloc](https://pub.flutter-io.cn/packages/bloc)
//  * [flutter_bloc](https://pub.flutter-io.cn/packages/flutter_bloc)
class SampleFlutterBloc1 extends StatelessWidget {
  const SampleFlutterBloc1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider<ThemeBloc>(
      create: (context) => ThemeBloc(ThemeData.light()),
      child: BlocBuilder<ThemeBloc, ThemeData>(
        builder: (context, theme) {
          return MaterialApp(
            title: "Flutter Demo",
            home: BlocProvider(
              create: (context) => MyCounterBloc(0),
              child: CounterPage(),
            ),
            theme: theme,
          );
        },
      ),
    );
  }
}

class CounterPage extends StatelessWidget {
  const CounterPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Counter"),
      ),
      body: BlocBuilder<MyCounterBloc, int>(
        builder: (context, count) {
          return Text(
            "$count",
            style: TextStyle(fontSize: 24.0),
          );
        },
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                // 计数器加法
                BlocProvider.of<MyCounterBloc>(context).add(MyCounterEvent.increment);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.remove),
              onPressed: () {
                // 计数器减法
                BlocProvider.of<MyCounterBloc>(context).add(MyCounterEvent.decrement);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.update),
              onPressed: () {
                //修改主题
                BlocProvider.of<ThemeBloc>(context).add(ThemeEvent.toogle);
              },
            ),
          )
        ],
      ),
    );
  }
}

enum MyCounterEvent { increment, decrement }

class MyCounterBloc extends Bloc<MyCounterEvent, int> {
  MyCounterBloc(int initialState) : super(initialState);

  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(MyCounterEvent event) async* {
    switch (event) {
      case MyCounterEvent.increment:
        yield state + 1;
        break;
      case MyCounterEvent.decrement:
        yield state - 1;
        break;
    }
  }
}

enum ThemeEvent { toogle }

class ThemeBloc extends Bloc<ThemeEvent, ThemeData> {
  ThemeBloc(ThemeData initialState) : super(initialState);

  @override
  // TODO: implement initialState
  ThemeData get initialState => ThemeData.light();

  @override
  Stream<ThemeData> mapEventToState(ThemeEvent event) async* {
    switch (event) {
      case ThemeEvent.toogle:
        yield state == ThemeData.dark() ? ThemeData.light() : ThemeData.dark();
        break;
    }
  }
}
